Conditions | 1 |
Paths | 2 |
Total Lines | 105 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* global API */ |
||
25 | window.contextMenu = (function () { |
||
26 | 'use strict'; |
||
27 | function initMenus() { |
||
28 | API.contextMenus.create({ |
||
29 | id: 'autoFill:', |
||
30 | title: 'Auto fill', |
||
31 | contexts: ['all'] |
||
32 | }); |
||
33 | |||
34 | API.contextMenus.create({ |
||
35 | id: 'copy:User', |
||
36 | title: 'Copy username', |
||
37 | contexts: ['all'] |
||
38 | }); |
||
39 | |||
40 | API.contextMenus.create({ |
||
41 | id: 'copy:Pass', |
||
42 | title: 'Copy password', |
||
43 | contexts: ['all'] |
||
44 | }); |
||
45 | |||
46 | |||
47 | API.contextMenus.create({ |
||
48 | id: 'copy:Url', |
||
49 | title: 'Copy URL', |
||
50 | contexts: ['all'] |
||
51 | }); |
||
52 | /* API.contextMenus.create({ |
||
53 | id: 'copy:OTP', |
||
54 | title: 'Copy OTP', |
||
55 | contexts: ['all'] |
||
56 | });*/ |
||
57 | } |
||
58 | |||
59 | function createMenuItem(parentId, id, label, clickcb) { |
||
60 | API.contextMenus.create({ |
||
61 | id: id, |
||
62 | title: label, |
||
63 | contexts: ["all"], |
||
64 | parentId: parentId, |
||
65 | onclick: clickcb |
||
66 | }); |
||
67 | } |
||
68 | |||
69 | function itemClickCallback(menu_action, login) { |
||
70 | var action = menu_action.menu.split(':', 1)[0]; |
||
71 | |||
72 | if (action === 'copy') { |
||
73 | copyTextToClipboard(login[menu_action.field]); |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | if (action === 'autoFill') { |
||
78 | API.tabs.query({active: true, currentWindow: true}).then(function (tabs) { |
||
79 | API.tabs.sendMessage(tabs[0].id, {method: "enterLoginDetails", args: login}); |
||
80 | }); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | function copyTextToClipboard(text) { |
||
85 | var copyFrom = document.createElement("textarea"); |
||
86 | copyFrom.textContent = text; |
||
87 | var body = document.getElementsByTagName('body')[0]; |
||
88 | body.appendChild(copyFrom); |
||
89 | copyFrom.select(); |
||
90 | document.execCommand('copy'); |
||
91 | body.removeChild(copyFrom); |
||
92 | } |
||
93 | API.contextMenus.removeAll(); |
||
94 | initMenus(); |
||
95 | |||
96 | return { |
||
97 | setContextItems: function (logins) { |
||
98 | |||
99 | var fields = [ |
||
100 | {menu: 'autoFill:', field: 'autoFill'}, |
||
101 | {field: 'username', menu: 'copy:User'}, |
||
102 | {field: 'password', menu: 'copy:Pass'}, |
||
103 | {field: 'url', menu: 'copy:Url'}, |
||
104 | // {field: 'totp', menu: 'copy:OTP'} |
||
105 | ]; |
||
106 | API.contextMenus.removeAll(); |
||
107 | initMenus(); |
||
108 | |||
109 | for (var i = 0; i < logins.length; i++) { |
||
110 | var login = logins[i]; |
||
111 | login.autoFill = true; |
||
112 | for (var f = 0; f < fields.length; f++) { |
||
113 | var field = fields[f]; |
||
114 | if (field['field'] === 'totp' && login.otp) { |
||
115 | login.totp = login.otp.secret; |
||
116 | } |
||
117 | if (login[field['field']]) { |
||
118 | createMenuItem(field['menu'], field['menu'] + ':' + login.guid, login.label, (function (field, login) { |
||
119 | return function () { |
||
120 | itemClickCallback(field, login); |
||
121 | }; |
||
122 | })(field, login)); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | }()); |